1708D - Difference Array - CodeForces Solution


brute force sortings *1900

Please click on ads to support us..

C++ Code:

// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h>

#define endl "\n"
#define x first
#define y second

using namespace std;
typedef long long ll;
typedef pair<int,int> PII;

const int N = 1000010;

int a[N];
int T,n,m;
int dx[4] = {0,1,-1,0},dy[4] = {1,0,0,-1};

void solve(){
	vector<int> a,b;
	cin >> n;
	int cnt = 0;
	for(int i=1;i<=n;i++){
		int x;
		cin >> x;
		if(!x) cnt++;
		else a.push_back(x);
	}
	while(a.size()>1){
		b.clear();
		if(cnt) b.push_back(a[0]),cnt--;
		for(int i=1;i<a.size();i++){
			int d = a[i] - a[i-1];
			if(!d) cnt++;
			else b.push_back(d);
		}
		sort(b.begin(),b.end());
		a = b;
	}
	if(!a.size()) cout << 0 << endl;
	else cout << a[0] << endl;
}

int main()
{
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	cin >> T;
	while(T--)
	solve();
	
    return 0;
}


Comments

Submit
0 Comments
More Questions

340A - The Wall
377A - Maze
500A - New Year Transportation
908D - New Year and Arbitrary Arrangement
199A - Hexadecimal's theorem
519C - A and B and Team Training
631A - Interview
961B - Lecture Sleep
522A - Reposts
1166D - Cute Sequences
1176A - Divide it
1527A - And Then There Were K
1618E - Singers' Tour
1560B - Who's Opposite
182B - Vasya's Calendar
934A - A Compatible Pair
1618F - Reverse
1684C - Column Swapping
57C - Array
1713D - Tournament Countdown
33A - What is for dinner
810A - Straight A
1433C - Dominant Piranha
633A - Ebony and Ivory
1196A - Three Piles of Candies
299A - Ksusha and Array
448B - Suffix Structures
1092B - Teams Forming
1166C - A Tale of Two Lands
544B - Sea and Islands